//
//  BCRecommendListVC.m
//  BananaChat
//
//  Created by 于洪东 on 2019/11/1.
//  Copyright © 2019 香蕉. All rights reserved.
//

#import "BCRecommendListVC.h"
#import "BCRecommendTableCell.h"
#import "BCRecommendVM.h"
#import "BCRecommendVideoModel.h"
#import "BCVideoDetailVC.h"

@interface BCRecommendListVC ()<UITableViewDelegate,UITableViewDataSource,BCRecommendVMDelegate>

@property (nonatomic,strong) UITableView *listView;
@property (nonatomic,strong) NSMutableArray *dataArray;
@property (nonatomic,strong) BCRecommendVM *vm;
@property (nonatomic,assign) int currentPage;

@end

@implementation BCRecommendListVC

#pragma mark - ♻️life cycle
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self setupUI];
    [self setRefresh];
}

#pragma mark - 🔄overwrite
-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
}

#pragma mark - 🚪public

-(void)setRefresh{
    self.currentPage = 1;
    
    __weak typeof(self) weakSelf = self;
    self.listView.mj_header = [MJRefreshNormalHeader headerWithRefreshingBlock:^{
        weakSelf.currentPage = 1;
        [weakSelf requestData];
    }];
    [self.listView.mj_header beginRefreshing];
    
    self.listView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        weakSelf.currentPage++;
        [weakSelf requestData];
    }];
}

-(void)requestData{
    [self.vm requestWithPageSize:@"20" gender:@"女" pageIndex:[NSString stringWithFormat:@"%d",self.currentPage]];
}

#pragma mark - 🍐delegate

#pragma mark - BCRecommendVMDelegate
-(void)requestSuccess:(BOOL)success data:(NSMutableArray *)data{
    [self.listView.mj_header endRefreshing];
    [self.listView.mj_footer endRefreshing];
    if (success) {
        self.dataArray = data;
        [self.listView reloadData];
    }
}

#pragma mark - UITableViewDelegate/UITableViewDataSource
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return self.dataArray.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    BCRecommendTableCell *cell = [tableView dequeueReusableCellWithIdentifier:@"BCRecommendTableCell" forIndexPath:indexPath];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    [cell fillCellWithModel:self.dataArray[indexPath.row]];
    return cell;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return SCREEN_WIDTH;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    BCVideoDetailVC *detailVC = [[BCVideoDetailVC alloc]init];
    [self.navigationController pushViewController:detailVC animated:YES];
}

#pragma mark - ☎️notification

#pragma mark - 🎬event response

#pragma mark - 🔒private

#pragma mark - 🌲setupUI
-(void)setupUI{
    [self.view addSubview:self.listView];
    [self.listView registerClass:[BCRecommendTableCell class] forCellReuseIdentifier:@"BCRecommendTableCell"];
    
    [self.listView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self.view);
    }];
    
}

#pragma mark - ☸getter and setter
-(UITableView *)listView{
    if (_listView == nil) {
        _listView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStylePlain];
        _listView.backgroundColor = NTBackGroundColor;
        _listView.delegate = self;
        _listView.dataSource = self;
        _listView.separatorStyle = UITableViewCellSeparatorStyleNone;
    }
    return _listView;
}

-(NSMutableArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSMutableArray array];
    }
    return _dataArray;
}

-(BCRecommendVM *)vm{
    if (_vm == nil) {
        _vm = [[BCRecommendVM alloc]init];
        _vm.delegate = self;
    }
    return _vm;
}


@end
